<template>
  <el-dialog
    v-loading="loading"
    :visible.sync="dialog"
    title="填写指引"
    :close-on-click-modal="false"
    width="550px"
    append-to-body
    @close="cancel"
  >
    <div>
      <el-form ref="writeGuideForm" :model="writeGuide" :rules="rules" size="small" label-width="180px">
        <el-form-item label="正确示例：" prop="correctEx">
          <el-input v-model="writeGuide.correctEx" size="mini" style="width: 280px;" />
        </el-form-item>
        <el-form-item label="错误示例：" prop="errorEx">
          <el-input v-model="writeGuide.errorEx" size="mini" style="width: 280px;" />
        </el-form-item>
        <el-form-item v-if="writeGuide.validateFlag" label="是否启用：">
          <el-select v-model="writeGuide.validateFlag" size="mini" style="width: 280px">
            <el-option key="Y" label="Y" value="Y" />
            <el-option key="N" label="N" value="N" />
          </el-select>
        </el-form-item>
      </el-form>
    </div>
    <div slot="footer" class="dialog-footer">
      <el-button size="mini" type="text" @click="cancel">取消</el-button>
      <el-button size="mini" type="primary" @click="doSubmit">提交</el-button>
    </div>
  </el-dialog>
</template>

<script>
import { saveWriteGuide } from '@/api/api'
import { Message } from 'element-ui'

export default {
  props: {
    parent: {
      type: Object,
      default: null
    }
  },
  data() {
    return {
      dialog: false,
      writeGuide: {},
      loading: false,
      rules: {
        correctEx: [
          { required: true, message: '请填写正确示例', trigger: 'blur' }
        ],
        errorEx: [
          { required: true, message: '请填写错误示例', trigger: 'blur' }
        ],
        validateFlag: [
          { required: true, message: '请选择是否启用', trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    cancel() {
      this.writeGuide = {}
      this.$refs.writeGuideForm.resetFields()
      this.dialog = false
    },
    doSubmit() {
      this.$refs.writeGuideForm.validate((valid) => {
        if (valid) {
          this.loading = true
          saveWriteGuide(this.writeGuide).then(_ => {
            this.parent.toQuery()
            Message({
              message: '操作成功。',
              type: 'success',
              duration: 3 * 1000
            })
            this.loading = false
            this.cancel()
          }, error => {
            console.log(error)
            this.loading = false
          })
        }
      })
    }
  }
}
</script>

